<template>
  <div class="side-wrap" :class="collapse ? 'side-wrap--collapse' : ''">
    <div class="input-wrap">
      <el-input
        class="input-box-content"
        v-model="text"
        placeholder="字典描述或名称"
        :suffix-icon="CIconSearch"
        @input="handleInputChange"
      ></el-input>
    </div>
    <div class="field-wrap">
      <div
        class="field-item"
        :class="{ 'field-item--select': selectData == item.id }"
        @click="handClick(item)"
        v-for="(item, index) in localVal"
        :key="index"
      >
        <div class="field-label" v-ellTooltip>{{ item.dataDesc }}</div>
        <!-- <div class="field-key" v-ellTooltip>{{ item.dataType }}</div> -->
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { withDefaults, defineProps, ref, watch, nextTick, defineEmits } from 'vue';
import { IDictTypeItem } from '@/apis/system/model/dictModel';

import CIconSearch from '@/components/CIconSearch/index.vue';
interface IProps {
  list: IDictTypeItem[];
  collapse: boolean;
}
const props = withDefaults(defineProps<IProps>(), {});
const emit = defineEmits(['select']);
const localVal = ref(props.list);
const selectData = ref('');
watch(
  () => props.list,
  val => {
    localVal.value = val;
    selectData.value = val.length ? val[0].id : '';
  }
);
watch(
  () => props.collapse,
  val => {
    if (!val) {
      nextTick(() => {
        const el = document.querySelector('.field-wrap');
        const index = localVal.value.findIndex(n => n.id === selectData.value);
        index > -1 && el && (el.scrollTop = index * 36);
      });
    }
  }
);

const text = ref('');
const handClick = (item: IDictTypeItem) => {
  selectData.value = item.id;
  emit('select', item);
};
const handleInputChange = (val: string) => {
  //
  if (val) {
    let arr = props.list.filter(item => item.dataType.includes(val) || item.dataDesc.includes(val));
    localVal.value = arr;
  } else {
    localVal.value = props.list;
  }
};
</script>
<style lang="scss" scoped>
.side-wrap {
  width: 240px;
  background: #fff;
  height: 100%;
  transition: width 0.3s;
  &.side-wrap--collapse {
    width: 0;
  }
  .input-wrap {
    width: 100%;
    overflow: hidden;
    .input-box-content {
      margin: 12px 16px;
      width: calc(100% - 32px);
    }
  }
  .field-item {
    padding: 0 13px 0 16px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
    position: relative;
    transition: background 0.2s;
    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 0;
      width: 2px;
      height: 0;
      background: #03ac82;
      transition: height 0.3s;
    }
    &:hover {
      background: #f3f7fb;
    }
  }
  .field-item--select {
    background: #f3f7fb;
    &::before {
      height: 36px;
    }
  }
  .field-wrap {
    height: calc(100% - 58px);
    overflow-y: scroll;
  }
  .field-label {
    font-size: 14px;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    color: #435970;
    line-height: 22px;
    // flex-shrink: 0;
    // margin-right: 10px;
    // min-width: 120px;
  }
  .field-key {
    font-size: 12px;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    color: #7a8a99;
    line-height: 20px;
  }
}
</style>
